function trim(val)
{
	 return val.replace(/(^\s*)|(\s*$)/g, "");  
}
 
function isMaxLen(o)
{  
	 var nMaxLen=o.getAttribute? parseInt(o.getAttribute("maxlength")):"";  
	 if(o.getAttribute && o.value.length>nMaxLen)
	 {      
	 	  o.value=o.value.substring(0,nMaxLen);  
	 }  
}
 
 
  
function selectAll(){
	var checkall = document.getElementById('checkall');
	var listsize = document.getElementById('listsize').value;
	for(var i=0;i<listsize;i++){
		if(checkall.checked){
			document.getElementById('checkone'+i).checked = true;
		}else{
			document.getElementById('checkone'+i).checked = false;
		}
	}
}

function judgeCheck()
{
    var pkid_string = '';
		var listsize = document.getElementById('listsize').value;
		for(var i=0;i<listsize;i++)
		{
				if(document.getElementById('checkone'+i).checked)
				{
					pkid_string += document.getElementById('checkone'+i).value+'|';
				}
		}
    return pkid_string;
}

//var one_member_price ='';		

function setAllPrice()
{
   
   var price = document.getElementById("s_price").value; 
   //alert(price);   
   if(trim(price)!='')
   {
       var allprice = trim(price);     
       document.getElementById('allprice').value = allprice;
       var rdata = Math.round(Math.random()* 10000);       
       var udata ='sale_price='+allprice+'&rdata='+rdata;       
       jQuery.ajax({
				   type: 'POST',
				   url: '/program/admin/goods/memberPrice.jsp',
				   data: udata,
                   async:false,				   
				   success: function(msg){
				     document.getElementById('memberprice').value =msg; 
				   }
       });        
       document.getElementById("selmethod").value ='0';       
       document.indexForm.action='/doTradeReg.do';
	   document.getElementById('bpm_id').value = '9999';
	   document.getElementById("pkid").value = judgeCheck();
       //alert(document.getElementById("memberprice").value);
       document.indexForm.submit();
          
   }
   else
   {
                 
        var elem=document.indexForm.elements;
		    var all_news_text='';
		    for(i=0;i<elem.length;i++)
		    {
		        if(elem[i].type=="text" && elem[i].id.indexOf('set_price')>-1)
		        {
                 var one_price = elem[i].value;
                 if(one_price=='')
                 {
                    one_price ='0';                 
                 }
                 
                 //alert(one_price);						   
						     
						     var rdata = Math.round(Math.random()* 10000);
					       var udata ='sale_price='+one_price+'&rdata='+rdata; 
					       jQuery.ajax({
									   type: 'POST',
									   url: '/program/admin/goods/memberPrice.jsp',
									   data: udata,
                     async:false,									   
									   success: function(msg){
									     //one_member_price = msg;
                       document.getElementById('one_member_price').value = msg  
                          									   
									   }
					       }); 
					       var  one_member_price = document.getElementById('one_member_price').value;
					       //alert(one_member_price);     
    	   
		      	   all_news_text +=elem[i].name+'='+one_price+' ='+one_member_price+' :';
		        }
		    }
       document.getElementById("selmethod").value ='1';       
       document.getElementById("pk_price").value = all_news_text;      
       //alert(document.getElementById("pk_price").value );
       document.indexForm.action='/doTradeReg.do';
		   document.getElementById('bpm_id').value = '9999';
       document.indexForm.submit();
   }   
   
   //document.indexForm.submit();
    
} 


function setAllSend_inter()
{
   var send_inter = document.getElementById("s_send_inter").value; 
   if(trim(send_inter)!='')
   {
       var allsend_inter = trim(send_inter); 
       document.getElementById("send_intermethod").value ='0';      
       document.getElementById('allsend_inter').value = allsend_inter;
       document.indexForm.action='/doTradeReg.do';
		   document.getElementById('bpm_id').value = '4698';
		   document.getElementById("pkid").value = judgeCheck();
       document.indexForm.submit();
          
   }
   else
   {
                 
        var elem=document.indexForm.elements;
		    var all_news_text='';
		    for(i=0;i<elem.length;i++)
		    {
		        if(elem[i].type=="text" && elem[i].id.indexOf('send_inter')>-1)
		        {
                 var send_inter = elem[i].value;
                 if(send_inter=='')
                 {
                    send_inter ='0';                 
                 }
                  	   
		      	   all_news_text +=elem[i].name+'|'+send_inter+',';
		        }
		    }
       document.getElementById("send_intermethod").value ='1';       
       document.getElementById("pk_send_inter").value = all_news_text; 
       //alert(document.getElementById("pk_send_inter").value);     
       document.indexForm.action='/doTradeReg.do';
		   document.getElementById('bpm_id').value = '4698';
       document.indexForm.submit();
   }   
   
} 



function changeTime(val){

	var obj = document.getElementById(val+'_operating').value;
	if( obj == '' || obj.indexOf('-')>-1 || obj=='c' || obj=='d'){
		document.getElementById(val+'_g').style.display = 'none';
	}else{
		document.getElementById(val+'_g').style.display = 'inline';
	}
}

function updatestate(val){
	var oper = document.getElementById(val+'_operating').value;
	var listsize = document.getElementById('listsize').value;
	var ifcomment='';
	var pkid_string = '';
	for(var i=0;i<listsize;i++){
		if(document.getElementById('checkone'+i).checked){
			pkid_string += document.getElementById('checkone'+i).value+'|';
		}
	}
	document.getElementById('pkid').value = pkid_string;
	if(pkid_string == ''){
		alert('请选择要操作的信息!');
		return false;
	}
	if(oper =='')
	{
	  alert('请选择操作方式!');
	  return false;
	}
	else if(oper == '-')
	{
	   document.getElementById('pkid').value = pkid_string;
	   document.indexForm.action='/doTradeReg.do';
	   if(window.confirm('确认删除?'))
	   {
		  document.indexForm.submit();
	   }
	}
	else if(oper=='c' || oper=='d')
	{
	   
	   document.getElementById('pkid').value = pkid_string;
	   document.getElementById('state_code').value = oper;
	   document.getElementById('bpm_id').value = '4758';
	   document.indexForm.action='/doTradeReg.do';
	   if(window.confirm('确认提交吗?'))
	   {
		  document.indexForm.submit();
	   }
	}
	else
	{
	    document.getElementById('pkid').value = pkid_string;
		document.getElementById('up_operating').value = oper;
		document.getElementById('s_start_date').value = document.getElementById(val+'_start_date').value;
		document.getElementById('s_end_date').value = document.getElementById(val+'_end_date').value;
		document.indexForm.action='/doTradeReg.do';
		document.getElementById('bpm_id').value = '2491';
		if(window.confirm('确认提交吗?'))
		{
			document.indexForm.submit();
		}
	
	 }
}





function deleteOneInfo(pkid,bpm_id)
{
 
	if(window.confirm('确认删除吗？'))
	{
	  window.location.href='/doTradeReg.do?pkid='+pkid+'&bpm_id='+bpm_id;
 	}
}


//---------------- add goods js

	function setAlllevelPrice()
	{
			var sale_price=document.getElementById('sale_price').value;
			var eles = document.forms['addForm'].elements;
			for (var i = 0; i < eles.length; i++)
			{
				if (eles[i].type=="text" && eles[i].name=="level_price")
				{
           var s_price = sale_price*eles[i-1].value*0.01;
           s_price = s_price.toFixed(2);				 
					 eles[i].value=s_price;
 
				}
			}
	}
 

	function setlevelPrice(i)
	{
				var  s_price = document.getElementById('userLevelDiscount'+i).value*document.getElementById('sale_price').value*0.01;
				s_price = s_price.toFixed(2);				
				document.getElementById('level_price'+i).value=s_price;
	}


// link goods and fittings
function LinkGoods(val)
{
	 document.getElementById(val+'_link_table').style.display='block';
 
	 var cat_id = document.getElementById(val+'_cat_id').value; 
   var brand_id = document.getElementById(val+'_brand_id').value;
   var info=document.getElementById(val+'_keyword').value;
 
   var cust_id= document.getElementById('cust_id').value;
   Ti_goodsInfo.getGoodToSelect(val,cat_id,cust_id,info,brand_id,function(rtn){
    	  $(val+'_showSelect').innerHTML=rtn;
    });
}
 


 function moveInAll(val)
 {
  	var select1All = document.getElementById(val+"_select1").options;
		for(var i=0;i<select1All.length;i++)
		{
		    var flag=0;
		  	var select2All = document.getElementById(val+"_select2").options;
		  	for(var j=0;j<select2All.length;j++)
		  	{
		  		if(select1All[i].value==select2All[j].value)flag=1;
		  	}
		    if(flag==0)
			  document.getElementById(val+"_select2").options.add(new Option(select1All[i].text,select1All[i].value)); 
		}
}	
	
	

	function moveOutAll(val)
	{
		var select2All = document.getElementById(val+"_select2").options.length=0;
	}

	function moveInSelected(val)
	{
		 var select1All = document.getElementById(val+"_select1").options;
		 	for(var i=0;i<select1All.length;i++)
		 	{
			    var flag=0;
			  	var select2All = document.getElementById(val+"_select2").options;
			  	for(var j=0;j<select2All.length;j++)
			  	{
			  		if(select1All[i].value==select2All[j].value)flag=1;
			  	}
			    if(flag==0&&select1All[i].selected)
				  document.getElementById(val+"_select2").options.add(new Option(select1All[i].text,select1All[i].value)); 
			}
	}

	function moveOutSelected(val)
	{
	   var selectIndex = document.getElementById(val+"_select2").options.selectedIndex;
     if(selectIndex!=-1)
     {
         document.getElementById(val+"_select2").options[selectIndex] = null;  
     }
	   
	}





function subForm(){
	
		 
	//------  goods attr validate
	var attrSize = document.getElementById('attrsize').value;
	var attr_value='';
	for( var j = 0; j < attrSize; j++ )
	{ 
		var item;
		var attrStr ='';
		var attrId = document.getElementById('attr_id'+j).value;
		var attrName = document.getElementById('attr_name'+j).value;
		var conType = document.getElementById('con_type' + j).value;
		var defaultTag = document.getElementById('default_tag' + j).value;
		
		// select
		if(conType == '0')
		{ 
		  attrStr= document.getElementById('attr_value'+j).value;
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！'  );
 
				return;
			}  
		}
	 
	  // input
		if(conType == '1')
		{
				attrStr = document.getElementById('attr_value'+j).value; 
				if( attrStr == '' && defaultTag=='0')
				{
					alert('请输入' + attrName+'！');
 
					//document.getElementById('attr_id'+j).focus();
					return;
				}  
		}
		
		//chechbox
		if( conType == '2')
		{
			var valueSize = document.getElementById('valuesize'+j).value;
			for( var r = 0; r < valueSize; r++ ){
				 if( document.getElementById('attr_value'+j+'_'+r).checked){
					attrStr +=  document.getElementById('attr_value'+j+'_'+r).value + '|'; 
				 } 
			}  
			if( attrStr == '' && defaultTag=='0')
			{
				alert('请选择' + attrName+'！' );
				return;
			}  
		}
		if(attrStr != ''){
			item = attrId+"|"+attrStr;
			attr_value += item +',';
		}
	}
	
	document.getElementById('attr_desc').value = attr_value;
  
  if(trim(document.getElementById('goods_name').value)=="")
  {
      alert('请输入商品名称！');
 
      return; 
  }
  if(trim(document.getElementById('keywords').value)=='')
  {
      alert('请输入商品关键字！');
      return; 
  }
  if(trim(document.getElementById('goods_no').value)=='')
  {
      alert('请输入商品编号！');
      return; 
  }
  if(trim(document.getElementById('market_price').value)=='')
  {
      alert('请输入市场价！');
      return; 
  } 
  if(trim(document.getElementById('sale_price').value)=='')
  {
      alert('请输入销售价！');
      return; 
  }
  if(trim(document.getElementById('org_price').value)=='')
  {
      alert('请输入进价！');
      return; 
  }
  if(trim(document.getElementById('send_inter').value)=='')
  {
      alert('请输入赠送积分数！');
      return; 
  }
  if(trim(document.getElementById('buy_inter').value)=='')
  {
      alert('请输入积分购买数！');
      return; 
  }
  if(trim(document.getElementById('weight').value)=='')
  {
      alert('请输入商品重量！');
      return; 
  }
  if(document.getElementById('price_unit').value=='')
  {
      alert('请选择计价单位！');
      return; 
  }
  if(trim(document.getElementById('stock_in').value)=='')
  {
      alert('请输入库存初始量！');
      return; 
  }
  if(trim(document.getElementById('stock_down').value)=='')
  {
      alert('请输入最低库存量！');
      return; 
  }
 
  if(trim(document.getElementById('one_price').value)=='')
  {
      alert('请输入商品库存单价！');
      return; 
  }
 
  var member_price='';
  var level_num = document.getElementById('level_num').value;
  for(var k=0;k<level_num;k++)
 
  {
  	 member_price  +=document.getElementById('userLevelId'+k).value+'|'+document.getElementById('level_price'+k).value+',';  
  } 
  
  document.getElementById('member_price').value = member_price;
       
  var stock_in = document.getElementById("stock_in").value;
  var stock_flag = document.getElementById("stock_flag").value;
  
  // stock vary add  
  if(stock_flag =='0')
  {
     document.getElementById("stock_num").value =stock_in; 
     document.getElementById("now_num").value = stock_in;
  }                                                  
                                                       
  //stock vary update  
  if(stock_flag =='1')
  {
     document.getElementById("stock_num").value =stock_in;
     document.getElementById("before_num").value = document.getElementById("now_num").value;  
     document.getElementById("now_num").value = stock_in;
     var before_num = document.getElementById("before_num").value;
     var vary_num = parseInt(stock_in) - parseInt(before_num);
     document.getElementById("vary_num").value = vary_num;
     document.getElementById("stock_method").value =0;   
     //alert(vary_num);     
     if(vary_num == 0)
     {
        document.getElementById("stock_method").value = 1;      
     } 
  }
  
  //alert(document.getElementById("stock_method").value);  
  var state_flag = document.getElementById("state_flag").value; 
  
  if(state_flag =='1' )
  {
    document.getElementById("state_name").value ="审核通过"; 
  }  
  
 // alert(document.getElementById("state_name").value);                                            
  
  document.addForm.submit();    

}

function searchForm(){
	
	var class_attr=""; 
  if(document.getElementById("sort1").value!="")
  {
      class_attr = document.getElementById("sort1").value+"|"; 
  }

  if(document.getElementById("sort2").value!="")
  {
      class_attr += document.getElementById("sort2").value+"|"; 
  }

  if(document.getElementById("sort3").value!="")
  {
      class_attr += document.getElementById("sort3").value+"|"; 
  }
  
  document.getElementById("s_class_attr").value = class_attr;	 
	 
	//alert(document.getElementById("class_attr").value);
	document.indexForm .submit();
	
}
  
function showLinkGoods(val){
   var cat_id = document.getElementById(val+'_cat_id').value; 
   var brand_id = document.getElementById(val+'_brand_id').value;
   var info = document.getElementById(val+'_keyword').value;
   info = encodeURI(encodeURI(info)); 
   if(cat_id=='' && brand_id=='' && info=='')
   {
      alert("至少选择或输入一种查询条件！");
      return;   
   }   
   
   var data = Math.round(Math.random()* 10000);   
   var bURL = "&cat_id="+cat_id+"&brand_id="+brand_id+"&info="+info+"&type="+val+"&data="+data;
   var gURL = "/program/admin/goods/showMoreGoods.jsp?height=400&width=600"+bURL;  
   
   var title ="";
   if(val=='g')
   {
     title ="关联商品";  
   } else if(val=='p'){
     title ="关联配件";   
   }         
   
   TB_show(title,gURL,false);                                                       

}

function delIndexInfos( val ){
    var opr = document.getElementById(val+'_operating').value;
	var listsize = document.getElementById('listsize').value;
	var pkid_string = '';
	for(var i=0;i<listsize;i++){
		if(document.getElementById('checkone'+i).checked){
			pkid_string += document.getElementById('checkone'+i).value+'|';
		}
	}
	if(pkid_string == ''){
		alert('请至少选择一条!');
		return false;
	}else{
		if(opr == '-'){
			document.getElementById('pkid').value = pkid_string;
			document.indexForm.action='/doTradeReg.do';
			if(window.confirm('确认删除吗?')){
				document.indexForm.submit();
			}
		}
		if( opr == 'c' || opr == 'b'){
			document.getElementById('pkid').value = pkid_string;
			document.getElementById('bpm_id').value = '4758';
			document.getElementById('state_code').value = opr;
			document.getElementById('up_operating').value = opr;
			document.indexForm.action='/doTradeReg.do';
			if(window.confirm('确认此操作吗?')){
				document.indexForm.submit();
			}
		}
		
	}
	
}



/*
function audit(val){
	if(val == 1){
		document.getElementById('state_code').value = 'c';
	}
	if(val == 2){
		document.getElementById('state_code').value = 'b';
		alert(document.getElementById('state_code').value);
	}
	document.addForm.submit();
}
*/

function audit(){
	if(document.getElementById('state_code2').checked)
	{
	   if(document.getElementById('remark').value == ""){
	     alert('请输入不通过理由！');
		 return;
	   }
	}else{
	  document.getElementById('remark').value ='';
	}
	document.addForm.submit();
}




